import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, OnDestroy} from '@angular/core';
import {MineService} from "@mine/mine.service";
import {Router} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {Subject} from "rxjs/Subject";

@Component({
  selector: 'seed-more',
  templateUrl: './more.component.html',
  styleUrls: ['./more.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class MoreComponent implements OnInit, OnDestroy {

  public myFunction: any;
  public authFunction: any;
  public setFunction: any;
  public publishFunction: any;
  public authRole: any;
  public ngUnsubscribe: Subject<void> = new Subject<void>();

  constructor(
    private mineService: MineService,
    private router: Router,
    public toolsService: ToolsService,
    public author: AuthService,
    public imgService: ImageServeService
  ) {
    this.toolsService.showLoading();
    /**
     * @description 获取用户所具有的授权功能信息
     * @author baoww765
     * @date 2017-12-15
     * */
    this.mineService.getMoreIp().takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this.toolsService.hideLoading();
        this.authRole = res.data;
      }
    });
    /**
     * @description 获取更多功能个功能分类
     * @author baoww765
     * @date 2017-12-15
     * */
    this.mineService.getMoreFun().takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this.toolsService.hideLoading();
        const data = res.data;
        this.myFunction = data.functionList;
        this.authFunction = data.authList;
        this.setFunction = data.settingsList;
        this.publishFunction = data.publishList;
      }
    });
  }

  /**
   * @description 进入对应功能页面，
   * @author baoww765
   * @date 2017-12-15
   * @param routerlink: 点击的功能所对应路由, name：点击的功能名称，用于做判断
   * */
  goToDetail(routerlink, name) {
    if (routerlink) {
      if (name === '我的相册' || name === '我的视频') {
        this.router.navigate([routerlink], {queryParams: {userId: this.author.getUserId()}});
      } else if (name === '联系客服') {
        location.href = routerlink;
      } else {
        this.router.navigate([routerlink]);
      }
    } else {
      this.toolsService.presentConfirm('下载求苗APP,看更多精彩内容!');
    }
  }

  /**
   * @description 获取图片
   * @author baoww765
   * @date 2017-12-15
   * @param iconName 想要获取的图片完整名称
   * */
  getIcon(iconName: string) {
    return this.imgService.getIcon(iconName);
  }

  /**
   * @description 路由至相关页面,根据后台配置接口，路由过去
   * @author baoww765
   * @data 2018-01-03
   **/
  goToRelatedPage(service) {
    if (service) {
      this.router.navigateByUrl(service);
    } else {
      this.toolsService.presentConfirm('下载求苗APP，看更多精彩内容!');
    }
  }

  ngOnInit() {

  };

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
    this.toolsService.hideLoading();
  }

}
